package com.yidu.hbatsams.action;


import com.yidu.hbatsams.domain.AtsScheme;
import com.yidu.hbatsams.domain.vo.UserVo;
import com.yidu.hbatsams.service.AtsSchemeServicefx;
import com.yidu.hbatsams.utils.Config;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

import java.net.URLEncoder;


/**
 *  控制器层：导出
 *  封璇
 */
@Controller
@RequestMapping("/ExportWord")
public class ExportWord {
    @Resource
    Config config;
    @Resource
    private AtsSchemeServicefx atsSchemeServicefx;
    @RequestMapping("/expertWord")
    public void expertWord(Integer schemeId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        OutputStream os = response.getOutputStream();
        System.out.println(schemeId+"...");
        //查询数据
        AtsScheme scheme = atsSchemeServicefx.findById(schemeId);
       //获取路径
        String contextPath = config.getDomainUrl();
        XWPFDocument document = null;
      //获取方案名
        String name = scheme.getSchemeName();
      //方案名设置格式
        name = URLEncoder.encode(name,"UTF-8");
      //获取用户名
        UserVo userVo = (UserVo)request.getSession().getAttribute("user");
        //导出到附件
        response.setContentType("application/vnd.ms-word");
        response.setHeader("Content-Disposition", "attachment; filename="+name+".docx");
        document =atsSchemeServicefx.createWord(userVo.getUserName(),Integer.toString(schemeId),contextPath);
        System.out.println("document..............................."+document);
        document.write(os);

        document.close();

        os.flush();

        os.close();

    }
}
